package com.hualan.controller;

import com.github.pagehelper.PageInfo;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Questions;
import com.hualan.pojo.Result;
import com.hualan.service.QuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questions")

public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;

    //查询所有问题
    @GetMapping("/all")
    public Result getAllActiveQuestions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageResult<Questions> result = questionsService.getAllActiveQuestions(page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("系统繁忙，请稍后再试");
        }
    }


    //管理员删除问题
    @DeleteMapping("/delete/{id}")
    public Result deleteQuestion(@PathVariable Long id) {
        try {
            int result = questionsService.deleteByPrimaryKey(id);
            if (result > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("未找到对应记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }


    //批量删除问题
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.error("参数错误：ID 列表不能为空");
            }
            int result = questionsService.batchDelete(ids);
            return Result.success("成功删除 " + result + " 条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }

    //新增问题
    @PostMapping("/insert")
    public Result insertQuestion(@RequestBody Questions questions) {
        try {
            int result = questionsService.insertQuestion(questions);
            if (result > 0) {
                return Result.success("插入成功");
            } else {
                return Result.error("插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }

    //标题模糊查询

    @GetMapping("/selectByTitle")
    public Result selectByTitle(
            @RequestParam(required = false) String title,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        PageInfo<Questions> pageInfo = questionsService.selectByTitle(title, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    //根据id查询问题
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        try {
            Questions question = questionsService.selectByPrimaryKey(id);
            if (question != null) {
                return Result.success(question);
            } else {
                return Result.error("未找到对应记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }

}
